package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 使用指定构造器实例化对象
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Person  p = new Person("张三");
        System.out.println(p);

        /*
         * 反射机制
         * 获取类对象
         *
         */
        Class cls = Class.forName("reflect.Person");

        // 获取构造器
        Constructor constructor = cls.getConstructor(String.class);
        Object obj = constructor.newInstance("李四");
        System.out.println(obj);

        Constructor constructor1 = cls.getConstructor(String.class, int.class);
        Object obj1 = constructor1.newInstance("王五", 20);
        System.out.println(obj1);
    }
}
